package pers.mingshan.thread.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 缓存系统
 * @author mingshan
 *
 */
public class CacheDemo {
    private Map<String,Object> cache= new HashMap<String ,Object>();
    private ReadWriteLock rwl = new ReentrantReadWriteLock();

    public static void main(String[] args) {
        
    }

    public Object getData(String key) {
        rwl.readLock().lock();
        Object result = cache.get(key);

        try {
            if (result == null) {
                rwl.readLock().unlock();
                rwl.writeLock().lock();

                try {
                    result="aaaa";
                } finally {
                    rwl.writeLock().unlock();
                }

                rwl.readLock().lock();
            }
            
        } finally {
            rwl.readLock().unlock();
        }

        return result;
    }
}
